<?php


namespace App\Api\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;


class PromotionBargainLaunch extends Model
{
    use HasFactory;
    protected $guarded = [];
    protected $table = 'cmc_promotion_bargain_launch';

    public static function userBargainGoods($memberId,$field){

        return self::query()->where('member_id',$memberId)->get($field);
    }

    public static function getYearBragainGoods($bargain_id)
    {
        $time = date('Y-m-d',time()-86400);

        $data = PromotionBargain::where('bargain_id',$bargain_id)->get()->toArray();

        $count = count($data);

        for($i=0;$i<$count;$i++){
            $data[$i]['create_at'] = strtotime(str_replace('T',' ',$data[$i]['start_time']));
        }

        $all = [];

        for($j=0;$j<$count;$j++){
            if($data[$j]['create_at'] <$time && $data[$j]['create_at']<$time+86400){
                unset($data[$j]);
            }else{
                $all[$j] = [$data[$j]['goods_id'],$data[$j]['created_at']];
            }
        }

        return $all;
    }

    public static function getBragainGoods()
    {
        return PromotionBargain::select('bargain_id')->get()->toArray();
    }

    static public function bargainList($bargainId,$where)
    {
        $bargainArray = PromotionBargain::where('id',$bargainId)->get([$where])->toArray();

        $array = [];

        $count = count($bargainArray);

        for($i=0;$i<$count;$i++){

            $array = Goods::where('goods_id',$bargainArray[$i]['goods_id'])->select('goods_id','goods_name','goods_image','created_at')->get()->toArray();

        }

        return $array;
    }
}
